<template>

    
        <link rel="stylesheet" href="./src/assets/DomCss/DomCss/css/login.css">
    
    <div class="pure-g content">
        <div class="pure-form pure-u-1">
            <h4>请登录您的账号</h4>
            <fieldset class="pure-group">
                <input type="text" class="pure-input-1" placeholder="用户名" required autofocus v-model="info.Uname">
                <input type="password" class="pure-input-1" placeholder="密　码" required v-model="info.Upwd">
            </fieldset>
            <button class="pure-button pure-u-1 pure-button-primary" @click="login">登　录</button>
        </div>
    </div>
    <div class="pure-g copyright">
        <div class="pure-u-1"> 2023 TWStudio Admin Dashboard Template. </div>
    </div>

</template>

<script setup lang="ts">
import 'bootstrap/dist/css/bootstrap.min.css'
import { useUserStore } from '../../store/index.ts';
import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

//创建仓库实例
const userStore = useUserStore();
const router = useRouter();

// 创建一个响应式的数据
const info = ref({
    Uname: '',
    Upwd: ''
});

//创建注册函数
const login = () => {
    // 获取用户输入的账户、密码
    console.log(info.value.Uname);
    console.log(info.value.Upwd);
    // 判断用户输入的账户、密码是否为空
    if (info.value.Uname == '') {
        alert('用户名不能为空');
        return;
    }

    if (info.value.Upwd == '') {
        alert('密码不能为空');
        return;
    }
    // 验证密码长度
    if (info.value.Upwd.length < 6 || info.value.Upwd.length > 14) {
        alert('密码长度为6-14位');
        return;
    }

    // 发送 axios 请求
    // 注册成功后，跳转到登录页面
    axios.get('https://localhost:44387/api/Users/Login', {
        params: {
            ULname: info.value.Uname,
            ULpwd: info.value.Upwd
        }
    })
        .then(function (res) {
            console.log(res);

            console.log(res.data);
            if (res.data.CK_Code == 1) {

                alert(res.data.CK_Massage);
                //将用户编号保存到仓库中
                userStore.UserId = res.data.CK_Ulist[0].U_Id;
                console.log(res.data.CK_Ulist[0].U_Id);
                //用户编号是否存在
                SearchUid(res.data.CK_Ulist[0].U_Id)
            }
            else if (res.data.CK_Code == 0) {
                alert(res.data.CK_Massage);
            }
            else if (res.data.CK_Code == -1) {
                alert(res.data.CK_Massage);
            }
            else {
                alert('登录失败');
            }
        })
        .catch(function (error) {
            console.log(error);
        });

}
//通过用户编号是否完善信息
const SearchUid = (Uid: string) => {
    axios.get('https://localhost:44387/api/Users/SearchUid', {
        params: {
            Uid: Uid
        }
    })
        .then(res => {
            console.log(res.data);
            if (res.data > 0) {
                //说明用户已经完善过个人信息，需要跳转到主页面
                router.push({
                    path: '/Index',
                    query: {
                        Uid: Uid
                    }
                })
            }
            else {
                //说明用户没有完善过个人信息，需要跳转到个人信息完善页面
                router.push({
                    path: '/UserRegister',
                    query: {
                        Uid: Uid
                    }
                });
            }


        })
        .catch(error => {
            console.log(error);
        });
}
// const Reg = () => {
//     router.push('/Register')
// }

</script>
